package com.scwatch.core.web.back;


import com.scwatch.common.orm.RowSide;
import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.Program;
import com.scwatch.core.domain.User;
import com.scwatch.core.support.Context;
import com.scwatch.ext.domain.Collect;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

import static com.scwatch.core.constant.Constants.*;

/**
 * @title :  定时任务
 * @contnet :  列表 、保存、保存并返回、保存并新增
 *
 * */
@Controller
@RequestMapping("/core/schedule_job")
public class ScheduleJobController {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleJobController.class);




    @RequestMapping("list.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:schedule_job:list")
    public String list(
            @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
//        Integer siteId = Context.getCurrentSiteId();
//        Map<String, String[]> params = Servlets.getParamValuesMap(request,
//                Constants.SEARCH_PREFIX);
        Page<Collect> pagedList = null;//service.findAll(siteId, params, pageable);
        modelMap.addAttribute("pagedList", pagedList);

        return "core/schedule_job/schedule_job_list";
    }

    @RequestMapping("save.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:schedule_job:save")
    public String save(Program bean, String redirect,
                       HttpServletRequest request, RedirectAttributes ra) {
        Integer siteId = Context.getCurrentSiteId();
        User user = Context.getCurrentUser();
        bean.setTime(new Date());
        bean.setUser(user);



//        programService.save(bean, siteId, user);

//        logService.operation("opr.program.add", bean.getName(), null,
//                bean.getId(), request);
//        logger.info("save Program, name={}.", bean.getName());
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        if (Constants.REDIRECT_LIST.equals(redirect)) {

            return "redirect:list.do";
        } else if (Constants.REDIRECT_CREATE.equals(redirect)) {
            return "redirect:create.do";
        } else {
            ra.addAttribute(bean.getId());
            return "redirect:edit.do";
        }
    }


    @RequestMapping("create.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:schedule_job:create")
    public String create(
            Integer id,
            Integer position,
            @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
//        Program bean = programService.get(id);
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
//        RowSide<Program> side = programService.findSide(params, bean, position,
//                pageable.getSort());
//        modelMap.addAttribute("bean", bean);
//        modelMap.addAttribute("side", side);

        modelMap.addAttribute("position", position);
        modelMap.addAttribute(OPRT, EDIT);
        return "core/program/program_form";
    }

    @RequestMapping("edit.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:schedule_job:edit")
    public String edit(
            Integer id,
            Integer position,
            @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
//        Program bean = programService.get(id);
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
//        RowSide<Program> side = programService.findSide(params, bean, position,
//                pageable.getSort());
//        modelMap.addAttribute("bean", bean);
//        modelMap.addAttribute("side", side);

        modelMap.addAttribute("position", position);
        modelMap.addAttribute(OPRT, EDIT);
        return "core/program/program_form";
    }






}
